import os, shutil

from distutils.cmd import Command
from distutils import log
from docutils.core import publish_file

class build_docs(Command):
    description = "build documentation"
    user_options = [
        # ('optname=', None, ""),
    ]
    def initialize_options(self):
        pass
        
    def finalize_options(self):
        pass
        
    def run(self):
        """build end-user documentation."""
        if not os.path.exists('./build'):
            os.mkdir('./build')
            log.info("created build dir")
        if os.path.exists('./build/docs'):
            shutil.rmtree('./build/docs')
        os.mkdir("./build/docs")
        body = publish_file(open("./README.txt", 'r'),
                    destination=open("./build/docs/README.html", 'w'),
                    writer_name='html',
                    settings_overrides=dict(stylesheet_path='./setup/blogmaker.css',
                        strip_comments=True),
                    )
        log.info("published docs to: ./build/docs/README.html")
        